package com.turbo.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.turbo.dialog.DemoDialog;
import com.turbo.service.ProjectService;
import org.jetbrains.annotations.NotNull;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;

/**
 * @author: zwx
 * @time: 2021/3/20 17:57
 */
public class DemoAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
        Project project = anActionEvent.getProject();

        // applicationService：所有idea窗口都是同一个实例，即单例。 在plugin.xml中以 applicationService 注册
/*        ProjectService applicationService = ServiceManager.getService(ProjectService.class);
        String appTime = applicationService.doSomething();
        System.out.println(appTime);*/

        // projectService：每个idea窗口一个实例
        ProjectService projectService = ServiceManager.getService(Objects.requireNonNull(anActionEvent.getProject()), ProjectService.class);
        String time = projectService.doSomething();
        System.out.println(time);
        // Messages.showMessageDialog(time, "提示", Messages.getInformationIcon());

        // 窗口


        DemoDialog demoDialog = new DemoDialog();
        demoDialog.setTitle("Demo提示框");
        /*demoDialog.setListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str = "public " + e.getActionCommand() + " methodName() {return null;}";
                WriteCommandAction.runWriteCommandAction(project, () -> {
                    Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);
                    CaretModel caretModel = editor.getCaretModel();
                    int offset = caretModel.getOffset();
                    editor.getDocument().insertString(offset, str);
                });
            }
        });*/
        demoDialog.setEvent(anActionEvent);
        demoDialog.setLocationRelativeTo(demoDialog); //居中
        // 调整此窗口的大小，以适合其子组件的首选大小和布局。
        demoDialog.pack();
        demoDialog.setVisible(true);
    }

    @Override
    public void update(@NotNull AnActionEvent anActionEvent) {
        // 判断鼠标点击的是不是目录，如果psiFile为null则是目录
        PsiFile psiFile = anActionEvent.getData(PlatformDataKeys.PSI_FILE);
        anActionEvent.getPresentation().setEnabled(psiFile == null);
        // 获取鼠标点击的所在文件夹目录
/*        IdeView ideView = anActionEvent.getRequiredData(LangDataKeys.IDE_VIEW);
        anActionEvent.getPresentation().setEnabled(enabled);*/
    }
}
